package com.fc.advbox.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.fc.advbox.common.ResponseJson;
import com.fc.advbox.controller.admin.BaseController;
import com.fc.advbox.entity.UserTokenAcctEntity;
import com.fc.advbox.entity.UserTokenAddressEntity;
import com.fc.advbox.service.IUserTokenAcctService;
import com.fc.advbox.service.IUserTokenAddressService;
import com.fc.advbox.utils.Constant;
import com.fc.advbox.utils.RRException;
import com.fc.advbox.utils.validator.ValidatorUtils;
import com.fc.advbox.utils.validator.group.AddGroup;
import com.fc.advbox.utils.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by E7450 on 2018/7/29.
 */
@RestController
@RequestMapping("/api/user/token/address")
@Api(value="用户虚拟币地址接口", tags={"用户虚拟币地址接口"})
public class UserTokenAddressController extends BaseController {

    @Autowired
    private IUserTokenAddressService userTokenAddressService;

    @ApiOperation(value = "POST -- 查询当前用户的虚拟币地址列表",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>可选参数: userId: 用户ID, 可以为null，如果为null则查询当前登陆用户</i></li>" +
                    "<li><i>可选参数: type: 账号类型 : key / eos / pai</i></li>" +
                    "<li><i>默认参数: page: 1, 当前页，默认值1</i></li>" +
                    "<li><i>默认参数: limit: 每次查询个数，默认为20</i></li>" +
                    "</ul>")
    @PostMapping("/list")
    public ResponseJson doQueryList(@RequestBody JSONObject params) {
        //校对并赋值 offset和limit
        calculatePagination(params);
        if(!params.containsKey("userId") || params.get("userId") == null)
        {
            params.put("userId", getUserId());
        }
        if(params.get("type") != null && !Constant.TokenAccountType.verify(params.getString("type")))
        {
            throw new RRException("type的值不正确!");
        }

        List<UserTokenAddressEntity> entityList = userTokenAddressService.queryList(params);
        return ResponseJson.success(entityList);
    }

    /**
     * 保存用户虚拟币地址
     */
    @PostMapping("/save")
    @ApiOperation(value = "添加用户虚拟币账号", response = ResponseJson.class, httpMethod="POST",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>userId: 用户ID，可选，如果为null，则为自己创建 </i></li>" +
                    "<li><i>type: 账号类型 key / eos / pai， 必需 </i></li>" +
                    "<li><i>addressName: 备注，名称: 必填 </i></li>" +
                    "<li><i>address: 地址 : 必填</i></li>" +
                    "</ul>")
    public ResponseJson save(@RequestBody UserTokenAddressEntity entity){
        entity.setCreateTime(new Date());

        if(!Constant.TokenAccountType.verify(entity.getType()))
        {
            throw new RRException("type的值不正确!");
        }
        if(entity.getUserId() == null)
        {
            entity.setUserId(getUserId());
        }
        ValidatorUtils.validateEntity(entity, AddGroup.class);

        userTokenAddressService.save(entity);
        return ResponseJson.success();
    }

    /**
     * 更新用户虚拟币地址
     */
    @PutMapping("/update")
    @ApiOperation(value = "添加用户虚拟币账号", response = ResponseJson.class, httpMethod="PUT",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>id: 虚拟币账号ID， 必需 </i></li>" +
                    "<li><i>userId: 用户ID，可选，如果为null，则为自己创建 </i></li>" +
                    "<li><i>type: 账号类型 key / eos / pai， 必需 </i></li>" +
                    "<li><i>addressName: 备注，名称: 必填 </i></li>" +
                    "<li><i>address: 地址 : 必填</i></li>" +
                    "</ul>")
    public ResponseJson update(@RequestBody UserTokenAddressEntity entity){
        if(entity.getUserId() == null)
        {
            throw new RRException("userId不能为空!");
        }
        ValidatorUtils.validateEntity(entity, UpdateGroup.class);

        userTokenAddressService.update(entity);
        return ResponseJson.success();
    }

    @ApiOperation(value = "POST -- 批量删除用户虚拟币地址",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>ids: 虚拟币地址ID， 必需 </i></li>" +
                    "</ul>")
    @PostMapping("/delete")
    public ResponseJson doDelete(@RequestBody Long[] ids) {
        if(ids != null){
            for (Long id: ids) {
                userTokenAddressService.delete(id);
            }
        }
        return ResponseJson.success();
    }
}
